<?php

// this file accepts POST variables with an author's name, then it renders
// all of their posts in their custom category previews

// quick and dirty method. look at http://codex.wordpress.org/AJAX_in_Plugins 
// for correct method, as well as http://wordpress.stackexchange.com/questions/14999/cant-access-wordpress-functions-in-file-called-via-ajax
include ('../../../../wp-load.php');

$author = $_POST['author'];

// get all "author" and "photographer" custom field records and
// store in array sort alphabetically so that comparison works
$all_pairs = $wpdb->get_results(
	"SELECT post_id, meta_value 
	 FROM wp_postmeta 
	 WHERE meta_key = 'author' OR meta_key = 'photographer' OR  meta_key = 'photographers'
	 ORDER BY meta_value", 
	ARRAY_A);
	
$authors = sort_authors($all_pairs);

// display given authors posts if that author exists
if (array_key_exists($author, $authors)) {
	query_posts(array('post__in' => $authors[$author]));
	while (have_posts()) : the_post();
		echo '<div class="post row" id="', the_ID(), '">';
		category_preview();
		echo '</div>';
	endwhile;
	wp_reset_query();
}

?>